<template>
  <div class="app-container">
    <!-- <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords">
          <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div> -->
    <el-card shadow="never">
      <!-- <template #header>
        <div class="justify-between">
          <el-button
            v-if="permissions.create"
            type="primary"
            @click="
              () => {
                dialogType = true;
              }
            "
            >添加账户信息
          </el-button>
          <el-button v-if="permissions.applyRefund" v-loading="applyLoading" type="primary" @click="handelApply"
            >申请退还
          </el-button>
        </div>
      </template> -->
      <el-table v-loading="loading" :data="tableList" highlight-current-row>
        <el-table-column label="缴纳主体信息" prop="paymentName" align="center" min-width="180" />
        <el-table-column label="缴纳主体统一社会信用代码" prop="paymentCode" align="center" min-width="180" />
        <el-table-column label="收支方式" prop="guaranteeAccountTypeName" align="center" min-width="180" />
        <el-table-column label="状态" prop="statusName" min-width="100" align="center">
          <template #default="scope">
            <el-text v-if="scope.row.status == 0" type="warning">{{ scope.row.statusName }}</el-text>
            <el-text v-if="scope.row.status == 1" type="primary">{{ scope.row.statusName }}</el-text>
            <el-text v-if="scope.row.status == 2" type="danger">{{ scope.row.statusName }}</el-text>
            <el-tooltip
              v-if="scope.row.status == -1"
              class="box-item"
              effect="light"
              :content="scope.row.checkReason"
              placement="top-start"
            >
              <el-text type="danger">{{ scope.row.statusName }}</el-text>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="缴存类型" prop="depositeTypeName" align="center" min-width="120" />
        <el-table-column label="账户名称/公司名称" prop="guaranteeAccountName" align="center" min-width="160" />
        <el-table-column label="已缴纳/担保/保单金额" prop="paid" align="center" min-width="160" />
        <el-table-column label="有效起始日期" prop="startDate" align="center" min-width="160" />
        <el-table-column label="有效终止日期" prop="endDate" align="center" min-width="160" />
        <el-table-column label="是否已退还" prop="hasReturn" align="center" min-width="100">
          <template #default="scope">
            <span v-if="scope.row.hasReturn">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="操作" fixed="right" align="center" min-width="160">
          <template #default="scope">
            <el-button
              v-if="permissions.delete && scope.row.status <= 0"
              type="primary"
              link
              @click="handelDelete(scope.row)"
            >
              删除
            </el-button>
            <el-button
              v-if="permissions.update && scope.row.status <= 0"
              type="primary"
              link
              @click="handelEdit(scope.row)"
            >
              修改
            </el-button>
            <el-button
              v-if="permissions.detail && scope.row.status > 0"
              type="primary"
              link
              @click="handelDetail(scope.row)"
            >
              详情
            </el-button>
            <el-button
              v-if="permissions.print && scope.row.hasReturn"
              type="primary"
              link
              @click="handelPrint(scope.row)"
            >
              打印意见
            </el-button>
          </template>
        </el-table-column> -->
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageIndex"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
    <!-- <el-dialog v-model="dialogType" custom-class="dialog-type" width="250px">
      <template #header>
        <span> 请选择账户方式 </span>
      </template>
      <div class="div-btn-type" @click="handelCreate(1)"><CircleCheck /> 保证金账户</div>
      <div class="div-btn-type" @click="handelCreate(4)"><CircleCheck />银行保函</div>
      <div class="div-btn-type" @click="handelCreate(3)"><CircleCheck />工程保证保险</div>
      <div class="div-btn-type" @click="handelCreate(2)"><CircleCheck />工程担保公司保函</div>
    </el-dialog>
    <add-form :dialog-add-or-edit="dialogAddOrEdit" @send-close="handleSendClose"></add-form>
    <apply-form :dialog-apply="dialogApply" @send-close="handleSendClose"></apply-form>
    <detail :dialog-detail="dialogDetail" @send-close="handleSendClose"></detail>
    <print-template
      v-if="dialogPrint.visible"
      :dialog-print="dialogPrint"
      @send-close="handleSendClose"
    ></print-template> -->
  </div>
</template>

<script lang="ts" setup>
// import addForm from "./addForm.vue";
// import applyForm from "./applyRefund.vue";
// import detail from "./detail.vue";
// import printTemplate from "./printTemplate.vue";

import {
  QueryProjectGuaranteePayment,
  GetNeedRefundPayment,
  DeleteProjectGuaranteePayment,
} from "@/api/guarantee/index";
import { QueryPaymentForm } from "@/api/guarantee/types";

const props = defineProps({
  id: {
    type: Number,
    default: 0,
  },
});
const { id } = toRefs(props);

const permissions = ref({
  create: false,
  update: false,
  delete: false,
  detail: false,
  print: false,
  applyRefund: false,
});

const currentSystem = ref(localStorage.getItem("usertypeName"));
const isLumpContractor = ref(localStorage.getItem("isLumpContractor"));

if (currentSystem.value == "SysUser") {
}
if (currentSystem.value == "Manager") {
  permissions.value.create = true;
  permissions.value.update = true;
  permissions.value.detail = true;
  permissions.value.print = true;
  permissions.value.applyRefund = true;
  permissions.value.delete = true;
}
if (currentSystem.value == "Constructor" || currentSystem.value == "Commander" || currentSystem.value == "Corp") {
}

const total = ref(0);
const loading = ref(false);
const queryFormRef = ref(ElForm);
const tableList = ref<any[]>();
const applyLoading = ref(false);
const queryParams = reactive<QueryPaymentForm>({
  pageIndex: 1,
  pageSize: 20,
  keywords: "",
});
const dialogType = ref(false);

const dialogAddOrEdit = reactive<DialogOption>({
  visible: false,
});
const dialogApply = reactive<DialogOption>({
  visible: false,
});
const dialogDetail = reactive<DialogOption>({
  visible: false,
});
const dialogPrint = reactive<any>({
  visible: false,
});

//添加
const handelCreate = (type: number) => {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.title = "添加保证金账户信息";
  dialogAddOrEdit.footBtn = "保存";
  dialogAddOrEdit.type = type;
  dialogType.value = false;
};
//修改
const handelEdit = (row: any) => {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.id = row.id;
  dialogAddOrEdit.title = "修改保证金账户信息";
  dialogAddOrEdit.type = row.guaranteeAccountType;
  dialogAddOrEdit.footBtn = "保存";
};
//申请退还
const handelApply = () => {
  applyLoading.value = true;
  GetNeedRefundPayment()
    .then((res) => {
      if (res.success) {
        dialogApply.title = "申请退还";
        dialogApply.visible = true;
        dialogApply.id = res.data;
        dialogApply.footBtn = "确认申请";
      } else {
      }
    })
    .finally(() => {
      applyLoading.value = false;
    });
};
//详情
const handelDetail = (row: any) => {
  dialogDetail.visible = true;
  dialogDetail.id = row.id;
  dialogDetail.title = "保证金账户详情";
};
const handelDelete = async (row: any) => {
  let confirmDelete = false;
  await ElMessageBox.confirm("确定删除该保证金账户信息吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    confirmDelete = true;
  });
  if (!confirmDelete) return;
  DeleteProjectGuaranteePayment(row.id)
    .then((res) => {
      if (res.success) {
        handleQuery();
      }
    })
    .catch((err) => {
      console.log(err);
    });
};

const handelPrint = (row: any) => {
  //打印意见
  dialogPrint.visible = true;
  dialogPrint.dataInfo = row;
  dialogPrint.title = "打印意见";
  dialogPrint.footBtn = "打印";
};

//关闭弹窗
const handleSendClose = function (needRefresh: boolean) {
  dialogAddOrEdit.visible = false;
  dialogAddOrEdit.id = undefined;
  dialogDetail.visible = false;
  dialogDetail.id = undefined;
  dialogApply.visible = false;
  dialogApply.id = undefined;
  dialogPrint.visible = false;
  if (needRefresh) {
    handleQuery();
  }
};

/* 列表查询 */
const handleQuery = async () => {
  loading.value = true;
  queryParams.projectId = id.value;
  QueryProjectGuaranteePayment(queryParams)
    .then((res) => {
      tableList.value = res.data.list;
      queryParams.pageIndex = res.data.pageNum;
      queryParams.pageSize = res.data.pageSize;
      total.value = res.data.total;
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  handleQuery();
});
</script>

<style lang="less" scoped>
:deep(.detail .el-dialog__body) {
  height: var(--mobileAddFormHeight);
  overflow-y: auto;
}
:deep(.dialog-type .el-dialog__header) {
  background-color: #fff;
  border: 0;
}
:deep(.dialog-type .el-dialog__header span) {
  font-size: 16px;
}
:deep(.dialog-type .el-dialog__body) {
  padding: 0;
  padding-bottom: 1px;
}
.div-btn-type {
  display: flex;
  justify-content: center;
  margin: 15px;
  margin-top: 0;

  width: 220px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  border: 1px solid #e6e6e6;
}
.div-btn-type svg {
  width: 20px;
  margin-right: 5px;
}
.div-btn-type:hover {
  cursor: pointer;
  background-color: rgb(73, 143, 229);
  color: #fff;
}
</style>
